package com.example.android.supermemo.app;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

/**
 * Created by Dumrongrith on 8/5/2014.
 */
public class TTSTask extends AsyncTask<String,Void,Void> {
    private final String LOG_TAG = MainActivity.class.getSimpleName();

    @Override
    protected Void doInBackground(String... params) {

        try {
            final String TTS_BASE_URL = "http://translate.google.com/translate_tts?";
            final String LANGUAGE_PARAM = "tl";
            final String QUERY_PARAM = "q";
            final String ENCODING_PARAM = "ie";

            String language = Utility.SetLanguageTTS(params[0]);
            String message = params[1];
            String encoding = "UTF-8";

            MediaPlayer player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            Uri uri = Uri.parse(TTS_BASE_URL).buildUpon().appendQueryParameter(LANGUAGE_PARAM,language)
                    .appendQueryParameter(QUERY_PARAM,message).appendQueryParameter(ENCODING_PARAM,encoding)
                    .build();
            player.setDataSource(uri.toString());

            player.prepare();
            player.start();

        } catch (Exception e) {
            Log.e(LOG_TAG,"Can't use media player : ",e);
        }
        return null;
    }


}
